package com.tencent.qf.team.service.impl;

import com.tencent.qf.team.dal.CommdityCouponsNewstate;
import com.tencent.qf.team.dao.CommdityCouponsNewstateDao;
import com.tencent.qf.team.dto.request.CommdityCouponsCalculatePriceInfoList;
import com.tencent.qf.team.dto.request.CommdityCouponsValidUniqueIdList;
import com.tencent.qf.team.dto.response.CommdityCouponsCalculatePriceInfoResponse;
import com.tencent.qf.team.dto.response.CommdityCouponsValidUniqueIdResponse;
import com.tencent.qf.team.service.CommdityCouponsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
public class CommdityCouponsServiceImpl implements CommdityCouponsService {
    @Autowired
    private CommdityCouponsNewstateDao commdityCouponsNewstateDao;

    /**
     * 获取可领取券的商品
     *
     * @param request
     * @return
     */
    @Override
    public List<CommdityCouponsValidUniqueIdResponse> validUniqueIdList(CommdityCouponsValidUniqueIdList request) {
        List<CommdityCouponsValidUniqueIdResponse> result = new ArrayList<>();
        Set<String> stockIds = new HashSet<>();
        request.getRequestData().stream().forEach(e -> {
            stockIds.addAll(Arrays.asList(e.getStockIds()));
            result.add(new CommdityCouponsValidUniqueIdResponse(e.getUniqueId(), 0, e.getStockIds()));
        });
        String dateNow = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        List<CommdityCouponsNewstate> list = commdityCouponsNewstateDao.validUniqueIdList(stockIds, dateNow);
        if (!CollectionUtils.isEmpty(list)) {
            Set<String> stockIdList = new HashSet<>();
            list.stream().forEach(e -> {
                stockIdList.add(String.valueOf(e.getStockId()));
            });
            result.stream().forEach(e -> {
                Boolean contains = CollectionUtils.containsAny(stockIdList, Arrays.asList(e.getStockIds()));
                if (contains) {
                    e.setIsValid(1);
                }
                e.setStockIds(null);
            });
        }
        return result;
    }

    /**
     * 计算券后价格，并返回券详情
     *
     * @param request
     * @return
     */
    @Override
    public CommdityCouponsCalculatePriceInfoResponse calculatePriceInfo(CommdityCouponsCalculatePriceInfoList request) {
        String dateNow = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        List<CommdityCouponsNewstate> list = commdityCouponsNewstateDao.calculatePriceInfo(request.getStockIds(), dateNow);

        CommdityCouponsCalculatePriceInfoResponse response = new CommdityCouponsCalculatePriceInfoResponse();
        response.setUniqueId(request.getUniqueId());
        response.setSalePrice(request.getSalePrice());

        Integer cpsPrice = request.getCpsPrice();
        Integer salePrice = request.getSalePrice();
        CommdityCouponsNewstate stockInfo = null;
        if (!CollectionUtils.isEmpty(list)) {
            for (CommdityCouponsNewstate state : list) {
                //1.销售价>券门槛
                //2.cps券后价-券id的优惠金额>=0.01
                Integer calculatePrice = cpsPrice - state.getMoney().intValue();
                if (salePrice > state.getThreshold() && calculatePrice >= 0.01) {
                    stockInfo = state;
                    break;
                }
            }
        }

        if (stockInfo == null) {
            response.setCpsPrice(cpsPrice);
            response.setStockInfo(CollectionUtils.isEmpty(list) ? null : list.get(0));
        } else {
            response.setCpsPrice(cpsPrice - stockInfo.getMoney().intValue());
            response.setStockInfo(stockInfo);
        }
        return response;
    }
}
